package com.sk.waste.app.adapters

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.lc.smc.lib.interfaces.LCInterface
import com.lc.smc.lib.utils.LCBaseUtil
import com.lc.smc.lib.utils.LCTimeUtil
import com.sk.waste.app.R
import com.sk.waste.app.models.MarketModel
import kotlin.math.absoluteValue


/**
 *  @time 2025/01/07  15:37
 *  @desc 行情报价
 */
class MarketAdapter(private val context: Context, val type: Int, val list: List<MarketModel>, val callback: LCInterface.LcCallBack?) :
    RecyclerView.Adapter<MarketAdapter.ViewHolder>() {


    override fun getItemCount(): Int {
        return list.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MarketAdapter.ViewHolder {
        if (type == 0) {
            return ViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_market_main, parent, false))
        } else {
            return ViewHolder(LayoutInflater.from(context).inflate(R.layout.adapter_market, parent, false))
        }
    }

    @SuppressLint("NotifyDataSetChanged", "SetTextI18n", "ResourceType")
    override fun onBindViewHolder(holder: ViewHolder, @SuppressLint("RecyclerView") position: Int) {

        val item = list[position]
        holder.ttvName.text = item.productName
        val unit = if (item.unit.isEmpty()) {
            ""
        } else {
            "/" + item.unit
        }
        if (LCBaseUtil.isNull(item.hightPrice)) {
            holder.ttvPriceToday.text = "￥" + item.lowPrice + unit
        } else {
            holder.ttvPriceToday.text = "￥" + item.lowPrice + " ~ " + item.hightPrice + unit
        }

        if (!LCBaseUtil.isNull(item.unit)) {
            holder.ttvUnit.text = item.unit
        }
//        if (type == 0)//0 首页，1 行情报价页
//        {
//
//        } else {
//            holder.ttvPriceYesday.text = item.lastdayPrice
//        }

        if (!LCBaseUtil.isNull(item.differencePrice)) {
            val diffprice = item.differencePrice.toDouble()
            if (diffprice > 0) {
                holder.imgPrice.visibility = View.VISIBLE
                holder.imgPrice.setImageResource(R.mipmap.img_item_price_up)
                holder.ttvPricePoor.text = item.differencePrice
                holder.ttvPricePoor.setTextColor(context.getColor(R.color.colorPrimary))
            } else if (diffprice < 0) {
                holder.imgPrice.visibility = View.VISIBLE
                holder.imgPrice.setImageResource(R.mipmap.img_item_price_down)
                holder.ttvPricePoor.text = diffprice.absoluteValue.toString()
                holder.ttvPricePoor.setTextColor(context.getColor(R.color.color_green))
            } else {
                holder.imgPrice.visibility = View.INVISIBLE
                holder.ttvPricePoor.text = "平"
                holder.ttvPricePoor.setTextColor(context.getColor(com.lc.smc.lib.R.color.color_text_gray_g))
            }
        }

        if (!LCBaseUtil.isNull(item.updateTime)) {
            holder.ttvTime.text = item.updateTime.substring(0, 7)
        }

        if (position % 2 == 0) {
            holder.rootView.setBackgroundColor(context.getColor(R.color.white))
        } else {
            holder.rootView.setBackgroundColor(context.getColor(R.color.color_market_item))
        }
        holder.rootView.tag = position
        holder.rootView.setOnClickListener {
//            val currentSelect = holder.rootView.tag as Int
//            callback!!.callBack(holder.rootView.tag, -1)
        }

    }

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val ttvName: TextView = view.findViewById(R.id.item_market_name)
        val ttvPriceToday: TextView = view.findViewById(R.id.item_market_price_today)
        val ttvPriceYesday: TextView = view.findViewById(R.id.item_market_price_yestoday)
        val ttvUnit: TextView = view.findViewById(R.id.item_market_price_unit)
        val ttvPricePoor: TextView = view.findViewById(R.id.item_market_price_poor)
        val imgPrice: ImageView = view.findViewById(R.id.item_market_price_img)
        val ttvTime: TextView = view.findViewById(R.id.item_tender_time)
        val rootView: LinearLayout = view.findViewById(R.id.item_root_layout)
    }

}